package com.foreknow.controller;

import com.foreknow.bean.Guest;
import com.foreknow.service.GuestService;
import com.foreknow.service.impl.GuestServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        //ServletConfig这个对象会提供一些方法来获取到web.xml中配置的初始化参数
        //String pageSize = config.getInitParameter("pageSize");
        //如果要获取到全局的初始化参数，以上方法不适用
        String pageSize = config.getServletContext().getInitParameter("pageSize");
        System.out.println(Integer.parseInt(pageSize));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methed = req.getParameter("q");
        if ("list".equals(methed)) {
            list(req, resp);
        } else if ("delete".equals(methed)) {
            delete(req, resp);
        } else if ("update".equals(methed)){
            update(req, resp);
        } else if ("add".equals(methed)){
            add(req, resp);
        }
        else {
            edit(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型：text/html
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式(否则会产生乱码)  默认：iOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        //请求转发
        req.getRequestDispatcher("/AddMessage.jsp").forward(req,resp);
    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型：text/html
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式(否则会产生乱码)  默认：iOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取到HttpSession对象用于保存用户的信息
        HttpSession session = req.getSession();

        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        GuestService service = new GuestServiceImpl();
        List<Object> list = service.Query();
        //通过request.setAttribute("list",list);在服务器端保存list集合
        session.setAttribute("list", list);
        //请求转发
        //req.getRequestDispatcher("/list.jsp").forward(req,resp);
        //重定向资源到list.jsp
        //如果使用重定向，页面中没有数据，因为只有req发生一次请求的时候才能获取到数据所以要将list加上绝对地址
        resp.sendRedirect(req.getContextPath()+"/list.jsp");
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型：text/html
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式(否则会产生乱码)  默认：iOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");
        int num = Integer.parseInt(id);
        GuestService service = new GuestServiceImpl();
        boolean isRight = service.delete(num);
        if (isRight) {
            list(req, resp);
        } else {
            out.println("error...");
        }

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型：text/html
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式(否则会产生乱码)  默认：iOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        GuestService service = new GuestServiceImpl();
        String id = req.getParameter("id");
        int num = Integer.parseInt(id);
        Guest guest = service.IdQuery(num);
        //通过request.setAttribute("list",list);在服务器端保存list集合
        req.setAttribute("guest", guest);
        //转发资源到list.jsp
        req.getRequestDispatcher("/update.jsp").forward(req, resp);
    }

    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置从服务器端向客户端返回的内容的类型   MIME类型：text/html
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式(否则会产生乱码)  默认：iOS 8859-1
        req.setCharacterEncoding("UTF-8");
        //获取打印流用于向网页输出内容
        PrintWriter out = resp.getWriter();
        //获取请求参数
        GuestService service = new GuestServiceImpl();
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        Guest guest = service.IdQuery(id);
        String createdtime = guest.getCreatedtime();
        guest.setName(name);
        guest.setPhone(phone);
        guest.setEmail(email);
        guest.setTitle(title);
        guest.setContent(content);
        guest.setCreatedtime(createdtime);
        boolean isRight = service.modify(guest);
        if (isRight){
            list(req, resp);
        } else {
            out.println("error...");
        }
    }
}
